package com.example.lenovo.androidthreadtest;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int UPDATE_TEXT = 1;
    private TextView text;
    private Handler handler = new Handler() {
      public void handleMessage(Message msg) {
          switch (msg.what) {
              case UPDATE_TEXT:
                  text.setText("Nice to meet you");
                  break;
                  default:
                      break;
          }
      }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView)findViewById(R.id.text);
        Button button = (Button)findViewById(R.id.btn_change);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_change:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message massage = new Message();
                        massage.what = UPDATE_TEXT;
                        handler.sendMessage(massage);
                    }
                }).start();
                break;
            default:
                break;
        }
    }
}
